#include <stdio.h>
#include <stdlib.h>
#include <sched.h>

/*
 * tool to get process's scheduler
 * used systemcall:  int sched_getscheduler(pid_t pid)
 * sched_getscheduler() queries the scheduling policy currently applied to the process identified by pid.  
 * If pid equals zero, the policy of the calling process will be retrieved.
 *
 * Currently,  Linux  supports  the
 * following "normal" (i.e., non-real-time) scheduling policies:
 * SCHED_OTHER   the standard round-robin time-sharing policy;
 * SCHED_BATCH   for "batch" style execution of processes; and
 * SCHED_IDLE    for running very low priority background jobs.
 *
 * The  following "real-time" policies are also supported, for special time-critical applications that need precise control over the way in which runnable processes are
 * selected for execution:
 * SCHED_FIFO    a first-in, first-out policy; and
 * SCHED_RR      a round-robin policy.
 */

/*
 * for 2.6.16 kernel
 * #define SCHED_NORMAL		0
 * #define SCHED_FIFO		1
 * #define SCHED_RR		2
 * #define SCHED_BATCH		3
 */


/*
 * for 2.6.34
 * #define SCHED_NORMAL		0
 * #define SCHED_FIFO		1
 * #define SCHED_RR		2
 * #define SCHED_BATCH		3
 * SCHED_ISO: reserved but not implemented yet 
 * #define SCHED_IDLE		5
 * Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork 
 * #define SCHED_RESET_ON_FORK     0x40000000
 */

int main(int argc, char* argv[])
{
	//printf("%d,%d,%d\n",SCHED_OTHER, SCHED_BATCH, SCHED_IDLE);
	if (argc != 2)
	{
		printf("Usgae: %s <pid>\n", argv[0]);
		exit(1);
	}

	int pid =  atoi(argv[1]);
	int ret = sched_getscheduler(pid);
	//may need to do some adjustment according to different kernel verison
	switch(ret)
	{
		case 0:
			printf("0 SCHED_NORMAL\n");
			break;
		case 1:
			printf("1 SCHED_FIFO\n");
			break;
		case 2:
			printf("2 SCHED_RR\n");
			break;
		case 3:
			printf("3 SCHED_BATCH\n");
			break;
		case 4:
			break;
		case 5:
			printf("5 SCHED_IDLE\n");
			break;
	}

	return 0;
}
